import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { tap, map } from 'rxjs/operators';
import { STComponent, STColumn, STData } from '@delon/abc';
import { BusinessCheckingEditComponent } from './edit/edit.component';
@Component({
  selector: 'business-checking',
  templateUrl: './checking.component.html',
  styles: [`
    .search__form {
      margin-bottom: 0;
    }
  `]
})
export class BusinessCheckingComponent implements OnInit {
  queryStr: any = {
    offset: 0,
    limit: 10
  };
  data: any[] = [];
  loading = false;
  status = [
    { index: 0, text: '排班制', value: false, type: 'default', checked: false },
    {
      index: 1,
      text: '坐班制',
      value: false,
      type: 'processing',
      checked: false,
    },
    { index: 2, text: '自由打卡', value: false, type: 'success', checked: false },
    { index: 3, text: '弹性工作', value: false, type: 'error', checked: false },
  ];
  @ViewChild('st')
  st: STComponent;
  columns: STColumn[] = [
    { title: '考勤规则', index: 'rule' },
    { title: '考勤人员', index: 'name' },
    { title: '假期类型', index: 'holiday' },
    {
      title: '时长',
      index: 'duration'
    },
    {
      title: '单据抄送',
      index: 'documents'
    },
    {
      title: '是否扣薪',
      index: 'dockPay'
    },
    {
      title: '扣薪比例',
      index: 'payPproportion'
    },
    {
      title: '备注',
      index: 'note'
    },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          click: (item: any) => this.openEdit(item)
        },
        {
          text: '删除',
          click: (item: any) => this.remove(item._id)
        },
      ],
    },
  ];
  selectedRows: STData[] = [];
  description = '';
  totalCallNo = 0;
  expandForm = false;
  constructor(
    private http: _HttpClient,
    public msg: NzMessageService,
    private modal: ModalHelper,
    private modalSrv: NzModalService
  ) {}
  ngOnInit() {
    this.getData();
  }
  getData() {
    this.loading = true;
    this.queryStr.statusList = this.status
      .filter(w => w.checked)
      .map(item => item.index);
    if (this.queryStr.status !== null && this.queryStr.status > -1)
      this.queryStr.statusList.push(this.queryStr.status);
    this.http
      .get('api/checking/list/', this.queryStr)
      .pipe(
        map((res: any) =>
        res.objects.map(i => {
            return i;
          }),
        ),
        tap(() => (this.loading = false)),
      )
      .subscribe(res => (this.data = res));
  }
  remove(id) {
    this.modalSrv.confirm({
      nzTitle     : '是否确认删除?',
      nzContent   : '<b style="color: red;">删除后不可恢复</b>',
      nzOkText    : '是',
      nzOkType    : 'danger',
      nzOnOk      : () => {
        this.http.delete('api/checking/delete/', {id}).subscribe((res: any) => {
          if(res.success) {
            this.msg.success(`删除成功`);
            this.getData();
            this.st.clearCheck();
          }else{
            this.msg.error(`删除失败`);
          }
        }, () => {
          this.msg.error(`网络或服务器错误`);
        });
      },
      nzCancelText: '否',
      nzOnCancel  : () => console.log('Cancel')
    });
  }
  reset(ls: any[]) {
    for (const item of ls) item.value = false;
    this.getData();
  }
  // 编辑新增
  openEdit(i: any = {}) {
    this.modal.create(BusinessCheckingEditComponent, { i }, { size: 'md' })
      .subscribe(res => {
        if(i._id) {
          res['_id'] = i._id;
          this.http.patch('api/checking/update/', res).subscribe((res: any) => {
            if(res.success) {
              this.msg.success(`修改成功`);
              this.getData();
            }else{
              this.msg.error(`考勤已存在`);
            }
          }, () => {
            this.msg.error(`网络或服务器错误`);
          });
        }else {
          this.http.post('api/checking/add/', res).subscribe((res: any) => {
            if(res.success) {
              this.msg.success(`添加成功`);
              this.getData();
            }else{
              this.msg.error(`考勤已存在`);
            }
          }, () => {
            this.msg.error(`网络或服务器错误`);
          });
        }
      });
  }
}
